package com;

import entity.Book;
import mappers.BookMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.lf5.util.Resource;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

public class BookStoreServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        InputStream is = Resources.getResourceAsStream("Mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);

        String act = request.getParameter("act");


        if(act.equals("list")){
            List<Book> list = mapper.listAll();
            request.setAttribute("list", list);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/List.jsp");
            requestDispatcher.forward(request,response);
        } else if(act.equals("add")){
            response.sendRedirect(request.getContextPath()+"/Info.jsp");
        } else if(act.equals("edit")){
            Integer id = Integer.valueOf(request.getParameter("id"));
            Book book = sqlSession.selectOne("listOne",id);
            request.setAttribute("book",book);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/Info.jsp");
            requestDispatcher.forward(request,response);
        } else if(act.equals("save")){
            if(request.getParameter("id")==null||"".equals(request.getParameter("id"))){
                Book book = new Book();
                book.setBookName(request.getParameter("bookName"));
                book.setAuthor(request.getParameter("author"));
                book.setPrice(Double.parseDouble(request.getParameter("price")));
                book.setPress(request.getParameter("press"));
                String date=request.getParameter("pressDate");
                if(date==null||date.equals("")){
                    book.setPressDate(null);
                }else{
                    try {
                        book.setPressDate(Date.valueOf(date));
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("book",book);
                        request.setAttribute("ErrorMsg","时间格式错误,正确格式:yyyy-mm-dd");
                        request.getRequestDispatcher("/Info.jsp").forward(request,response);
                        return;
                    }
                }
                sqlSession.insert("InsertOne",book);
                sqlSession.commit();
                response.sendRedirect(request.getContextPath()+"/bookstore?act=list");
            }else{
                Integer id = Integer.parseInt(request.getParameter("id"));
                Book book = new Book();
                book.setId(id);
                book.setBookName(request.getParameter("bookName"));
                book.setAuthor(request.getParameter("author"));
                book.setPrice(Double.parseDouble(request.getParameter("price")));
                book.setPress(request.getParameter("press"));
                String date = request.getParameter("pressDate");
                if(date==null||date.equals("")){
                    book.setPressDate(null);
                }else{
                    try {
                        book.setPressDate(Date.valueOf(date));
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("book",book);
                        request.setAttribute("ErrorMsg","时间格式错误,正确格式:yyyy-mm-dd");
                        request.getRequestDispatcher("/Info.jsp").forward(request,response);
                        return;
                    }
                }
                sqlSession.update("UpdateOne",book);
                sqlSession.commit();
                response.sendRedirect(request.getContextPath()+"/bookstore?act=list");
            }
        } else if(act.equals("delete")){
            Integer id = Integer.valueOf(request.getParameter("id"));
            sqlSession.delete("DeleteOne",id);
            sqlSession.commit();
            response.sendRedirect(request.getContextPath()+"/bookstore?act=list");
        } else if(act.equals("exit")){
            HttpSession session = request.getSession();
            session.invalidate();
            response.sendRedirect(request.getContextPath()+"/Login.jsp");
        }
        sqlSession.close();
    }
}
